//
//  CachedLIView.m
//  SlideMenu
//
//  Created by khoa nguyen on 7/24/14.
//  Copyright (c) 2014 Aryan Ghassemi. All rights reserved.
//

#import "CachedLIView.h"

@implementation CachedLIView
{
    UIBezierPath *path;
    UIImage *incrementalImage; // (1)
    CGPoint pts[5]; // we now need to keep track of the four points of a Bezier segment and the first control point of the next segment
    uint ctr;
}

@synthesize  shapeIndex;

CGPoint first_point;
CGPoint last_point;

CGPoint topleftpoint;
CGPoint bottomrightpoint;

UIBezierPath *rectpath;

- (id)initWithCoder:(NSCoder *)aDecoder
{
    if (self = [super initWithCoder:aDecoder])
    {
        [self setMultipleTouchEnabled:NO];
        [self setBackgroundColor:[UIColor whiteColor]];
        path = [UIBezierPath bezierPath];
        [path setLineWidth:1.5];
        
    }
    
    shapeIndex= 0;
    ctr= -1;
    return self;
}

- (void)drawRect:(CGRect)rect
{
    [incrementalImage drawInRect:rect]; // (3)
    [path stroke];
}


- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [touches anyObject];
    CGPoint p = [touch locationInView:self];
    [path moveToPoint:p];
    
    first_point = p;
    
    ctr += 1;
    
    pts[ctr] = first_point;
    
}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [touches anyObject];
    CGPoint p = [touch locationInView:self];
    
    last_point = p;
    
    switch(shapeIndex){
        case 0:
            [path setLineWidth:1.5];
            [path removeAllPoints];
            [path moveToPoint:first_point];
            [path addLineToPoint:p];
            break;
        case 1:
            [path setLineWidth:1.5];
            [self calculatorLocationDrawing:first_point :p];
            
            [self drawRectangle:p];
            break;
        case 2:
            [path setLineWidth:1.5];
            [self calculatorLocationDrawing:first_point :p];
            
            [self drawCircle];
            break;
        case 3:
            [path setLineWidth:1.5];
            [self drawFreePen:p];
            
            //[path addLineToPoint:p];
            break;
        case 4:
            [path setLineWidth:10];
            [path addLineToPoint:p];
            break;
        default:
            break;
        
    }

   
    [self setNeedsDisplay];
    
}

-(void) drawFreePen:(CGPoint) p{
    
    ctr++;
    pts[ctr] = p;
    if (ctr == 4)
    {
        pts[3] = CGPointMake((pts[2].x + pts[4].x)/2.0, (pts[2].y + pts[4].y)/2.0); // move the endpoint to the middle of the line joining the second control point of the first Bezier segment and the first control point of the second Bezier segment
        
        [path moveToPoint:pts[0]];
        [path addCurveToPoint:pts[3] controlPoint1:pts[1] controlPoint2:pts[2]]; // add a cubic Bezier from pt[0] to pt[3], with control points pt[1] and pt[2]
        
        [self setNeedsDisplay];
        // replace points and get ready to handle the next segment
        pts[0] = pts[3];
        pts[1] = pts[4];
        ctr = 1;
    }
    
}

-(void) calculatorLocationDrawing: (CGPoint) p1 :(CGPoint) p2{
    if( p2.x > p1.x && p2.y > p1.y)
    {
        topleftpoint.x =p1.x;
        topleftpoint.y=p1.y;
        bottomrightpoint.x= p2.x - p1.x ;
        bottomrightpoint.y= p2.y - p1.y;

    }else{
        if( p2.x < p1.x && p2.y< p1.y)
        {
            topleftpoint.x = p2.x;
            topleftpoint.y = p2.y;
            bottomrightpoint.x = p1.x - p2.x;
            bottomrightpoint.y = p1.y - p2.y;
            
        }else{
            if( p2.y < p1.y)
            {
                
                topleftpoint.x =p1.x;
                topleftpoint.y=p2.y;
                bottomrightpoint.x=p2.x - p1.x;
                bottomrightpoint.y=p1.y - p2.y;
            }else{
                
                topleftpoint.x =p2.x;
                topleftpoint.y =p1.y;
                bottomrightpoint.x=p1.x - p2.x;
                bottomrightpoint.y=p2.y - p1.y;

            }
        }
    }

}

- (void) drawRectangle: (CGPoint) p  {
    path = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(topleftpoint.x, topleftpoint.y, bottomrightpoint.x, bottomrightpoint.y) cornerRadius:0.0];

    
}

- (void) drawCircle
{
   
    path = [UIBezierPath bezierPathWithOvalInRect:
            CGRectMake(topleftpoint.x , topleftpoint.y, bottomrightpoint.x, bottomrightpoint.y)];
}


- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event // (2)
{

    [self drawBitmap]; // (3)
    [self setNeedsDisplay];
    [path removeAllPoints]; //(4)
    
    ctr = -1;
}

- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
{
    [self touchesEnded:touches withEvent:event];
}

- (void)drawBitmap // (3)
{
    
    UIGraphicsBeginImageContextWithOptions(self.bounds.size, YES, 0.0);
    
    if( shapeIndex == 4)
    {
        [[UIColor whiteColor] setStroke];
    }
    else{
        [[UIColor blackColor] setStroke];
    }
    
    if (!incrementalImage) // first draw; paint background white by ...
    {
        UIBezierPath *rectpath = [UIBezierPath bezierPathWithRect:self.bounds]; // enclosing bitmap by a rectangle defined by another UIBezierPath object
        [[UIColor whiteColor] setFill];
        [rectpath fill]; // filling it with white
    }
    [incrementalImage drawAtPoint:CGPointZero];
    [path stroke];
    incrementalImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
}
*/

-(void) updateEventDrawing :(int) index{
    shapeIndex = index;
}

-(void) updateEventNew{
    incrementalImage = nil;
    
    [self setNeedsDisplay];
}

@end
